package sicau.carManager.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import sicau.carManager.entity.Notice;
import sicau.carManager.result.R;
import sicau.carManager.service.NoticeService;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ITcz
 * @since 2020-07-28
 */
@RestController
@RequestMapping("/notice")
@CrossOrigin
public class NoticeController {

    @Autowired
    NoticeService noticeService;

    @PostMapping("addNotice")
    @ApiOperation("管理员添加公告")
    public R addNotice(@ApiParam("标题") @RequestParam("title") String title,
                       @ApiParam("公告内容") @RequestParam("notice") String content){
        if (title == null || content == null) {
            return R.error("标题或公告内容为空");
        }
        if(noticeService.addNotice(title, content)) {
            return R.ok("添加成功");
        }else {
            return R.error(0,"添加失败");
        }
    }

    @PostMapping("uploadImage")
    @ApiOperation("上传公告里的图片")
    public R setImgUrl(@ApiParam("MultipartFile类型的图片信息(用form data方式上传)") @RequestParam("imgFile") MultipartFile file) throws IOException {
        if(file.isEmpty()) {
            return R.error(501, "参数为空");
        }
        // Get the file and save it somewhere
        byte[] bytes = file.getBytes();
        System.out.println(new String(bytes));
        //存放公告图片的文件夹地址
        String path = "/usr/carManager/javaImage/";
        File imgFile = new File(path);
        if (!imgFile.exists()) {
            //不存在文件则创建一个目录，路径名由当前File对象指定
            imgFile.mkdirs();
        }
        // 文件名称
        String fileName = file.getOriginalFilename();
        System.out.println(path + fileName);
        //在选定文件中写入图片
        try (FileOutputStream fos = new FileOutputStream(new File(path + fileName))) {
            int len = 0;
            fos.write(bytes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //value 的值为前端渲染图片的绝对路径(图片从/usr/local/javaImage/路径下取)
        String value = "http://113.54.11.32:8081/" + fileName;
        HashMap<String, Object> map = new HashMap<>();
        map.put("url", value);
        return R.ok(map);
    }

    @PostMapping("deleteNotice")
    @ApiOperation("删除公告")
    public R deleteNotice(@ApiParam("公告id") @RequestParam("id") Integer id){
        if(id == null) {
            return R.error(0, "参数为空");
        }
        if(noticeService.deleteNotice(id)){
            return R.ok("删除成功");
        }else {
            return R.error(0,"删除失败");
        }
    }

    @GetMapping("showNotice")
    @ApiOperation("展示公告")
    public R showNotice(@ApiParam("页码") @RequestParam("page") int page,
                        @ApiParam("每页显示公告数") @RequestParam("pageSize") int pageSize){
        if(page > 0 && pageSize > 0){
            Map<String, Object> map = new HashMap<String, Object>();
            IPage<Notice> iPage = noticeService.getAllNotice(page, pageSize);
            map.put("total",iPage.getTotal());
            map.put("result",iPage.getRecords());
            return R.ok(map);
        }else {
            return R.error(0,"参数非法");
        }
    }
}

